import pytest
import requests
import pymysql
import json
# def readjson():
#      with open(file="data.json",mode="r",encoding="utf-8") as f:
#          data=json.load(f)
#          return data
#
# class TestQrdd:
#     def setup_class(self):
#         print("用例初始化中----")
#     def teardown(self):
#         print("执行完毕")
#     token=None
#     oid=None
#     def test_dl(self):
#         url="http://10.59.9.12:7001/api/public/login"
#         headers={"content-type":"application/json"}
#         body={"username":"13811033962","password":"123456"}
#         res=requests.post(url=url,headers=headers,json=body)
#         print("登录成功:",res.json())
#         assert res.status_code==200
#         TestQrdd.token="Bearer "+res.json()['data']['token']
#     def test_xq(self):
#         url="http://10.59.9.12:7001/api/goods/view?id=32"
#         headers={"Auth-Token":TestQrdd.token}
#         res=requests.get(url=url,headers=headers)
#         print("商品详情:", res.json())
#         assert res.status_code == 200
#     def test_jr(self):
#         url = "http://10.59.9.12:7001/api/cart/add"
#         headers = {"content-type": "application/json","Auth-Token":TestQrdd.token}
#         body = {"id":32,"sku_id":"","num":1}
#         res = requests.post(url=url, headers=headers, json=body)
#         print("加入购物车:", res.json())
#         assert res.status_code == 200
#     def test_lb(self):
#         url="http://10.59.9.12:7001/api/cart?page=1"
#         headers = {"Auth-Token": TestQrdd.token}
#         res = requests.get(url=url, headers=headers)
#         print("购物车列表:", res.json())
#         assert res.status_code == 200
#         TestQrdd.oid=res.json()['data']['list'][0]['id']
#
#     # 确认订单接口全部正确,确认订单成功
#     @pytest.mark.parametrize("data",readjson())
#     def test_qrdd1(self,data):
#         # conn = pymysql.connect(user="root", password="yidong666", database="a3mall", host="10.59.9.12")
#         # cur = conn.cursor()
#         # cur.execute("select order_id from mall_order")
#         # oid = cur.fetchall()
#         # print(oid)
#         # lastoid = oid[-1][0]
#
#         url=f"http://10.59.9.12:7001/api/order/confirm?id={data.get(id)}&type=cart&shipping_type=1"
#         headers = {"Auth-Token": TestQrdd.token}
#         res = requests.get(url=url, headers=headers)
#         print("确认订单:", res.json())
#         assert res.status_code == 200
#         # assert lastoid+1==res.json()['data']['list'][0]['order_id']


    #
    # # 确认订单接口发货方式不存在,确认订单成功
    # def test_qrdd6(self):
    #     url=f"http://10.59.9.12:7001/api/order/confirm?id={TestQrdd.oid}&type=cart&shipping_type=VVV"
    #     headers = {"Auth-Token": TestQrdd.token}
    #     res = requests.get(url=url, headers=headers)
    #     print("确认订单:", res.json())
    #     assert res.status_code == 200
    #
    # # 确认订单接口发货方式参数缺失,确认订单成功
    # def test_qrdd2(self):
    #     url=f"http://10.59.9.12:7001/api/order/confirm?id={TestQrdd.oid}&type=cart"
    #     headers = {"Auth-Token": TestQrdd.token}
    #     res = requests.get(url=url, headers=headers)
    #     print("确认订单:", res.json())
    #     assert res.status_code == 200
    # #确认订单接口订单购物车商品id不存在,确认订单失败
    # def test_qrdd3(self):
    #     url=f"http://10.59.9.12:7001/api/order/confirm?id=><><>&type=cart&shipping_type=1"
    #     headers = {"Auth-Token": TestQrdd.token}
    #     res = requests.get(url=url, headers=headers)
    #     print("确认订单:", res.json())
    #     assert res.status_code == 200
    # #确认订单接口订单购物车商品id为0,确认订单失败
    # def test_qrdd4(self):
    #     url=f"http://10.59.9.12:7001/api/order/confirm?id=0&type=cart&shipping_type=1"
    #     headers = {"Auth-Token": TestQrdd.token}
    #     res = requests.get(url=url, headers=headers)
    #     print("确认订单:", res.json())
    #     assert res.status_code == 200
    #
    #

class TestZf:
    def setup_class(self):
        print("用例初始化中----")
    def teardown(self):
        print("执行完毕")
    token=None
    oid=None
    def test_dl(self):
        url="http://10.59.9.12:7001/api/public/login"
        headers={"content-type":"application/json"}
        body={"username":"13811033962","password":"123456"}
        res=requests.post(url=url,headers=headers,json=body)
        print("登录成功:",res.json())
        assert res.status_code==200
        TestZf.token="Bearer "+res.json()['data']['token']
    def test_xq(self):
        url="http://10.59.9.12:7001/api/goods/view?id=32"
        headers={"Auth-Token":TestZf.token}
        res=requests.get(url=url,headers=headers)
        print("商品详情:", res.json())
        assert res.status_code == 200
    def test_jr(self):
        url = "http://10.59.9.12:7001/api/cart/add"
        headers = {"content-type": "application/json","Auth-Token":TestZf.token}
        body = {"id":32,"sku_id":"","num":1}
        res = requests.post(url=url, headers=headers, json=body)
        print("加入购物车:", res.json())
        assert res.status_code == 200
    def test_lb(self):
        url="http://10.59.9.12:7001/api/cart?page=1"
        headers = {"Auth-Token": TestZf.token}
        res = requests.get(url=url, headers=headers)
        print("购物车列表:", res.json())
        assert res.status_code == 200
        TestZf.oid=res.json()['data']['list'][0]['id']

    # 确认订单接口全部正确,确认订单成功
    def test_qrdd(self):
        url=f"http://10.59.9.12:7001/api/order/confirm?id={TestZf.oid}&type=cart&shipping_type=1"
        headers = {"Auth-Token": TestZf.token}
        res = requests.get(url=url, headers=headers)
        print("确认订单:", res.json())
        assert res.status_code == 200

    def test_tjzf(self):
        url="http://10.59.9.12:7001/api/order/create"
        headers = {"content-type": "application/json","Auth-Token":TestZf.token}
        body={"id":TestZf.oid,"type":"cart","address_id":15,"store_id":1,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":"http://10.59.9.12:7001/wap/pages/cart/confirm?id=191&type=cart"}
        res=requests.post(url=url,headers=headers,json=body)
        print("提交并支付:",res.json())
        assert res.status_code == 200

